package org.javaboy.vhr.controller.system;

import org.javaboy.vhr.model.Hr;
import org.javaboy.vhr.model.Role;
import org.javaboy.vhr.service.HrService;
import org.javaboy.vhr.service.RoleService;
import org.javaboy.vhr.responseBean.ServerResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("system/hr")
public class HrController {

    @Autowired
    HrService hrService;
    @Autowired
    RoleService roleService;

    /**
     * 查询hr列表
     * @return
     */
    @RequestMapping("/")
    public ServerResponse getAllHr(String keywords){
        List<Hr> hrList = hrService.getAllHr(keywords);
        return ServerResponse.success("",hrList);
    }

    /**
     * 开启或禁用hr用户
     * @param hr
     * @param type
     * @return
     */
    @PutMapping("/{type}")
    public ServerResponse updateHr(@RequestBody Hr hr, @PathVariable String type){
        String successMsg = "修改成功";
        String errorMsg = "修改失败";
        if(hr.isEnabled() && "enabled".equals(type)){
            successMsg = "开启成功";
        }
        if(!hr.isEnabled() && "enabled".equals(type)){
            successMsg = "禁用成功";
        }
        if(hrService.updateHr(hr)==1){
            return ServerResponse.success(successMsg);
        }
        if(hr.isEnabled() && "enabled".equals(type)){
            errorMsg = "开启失败";
        }
        if(!hr.isEnabled() && "enabled".equals(type)){
            errorMsg = "禁用失败";
        }
        return ServerResponse.error(1000,errorMsg);
    }

    /**
     * 获取所有的角色
     * @return
     */
    @GetMapping("/getAllRoses")
    public ServerResponse getAllRoses(){
        List<Role> roleList = roleService.getAllRole();
        return ServerResponse.success("",roleList);
    }

    /**
     * 修改用户角色
     * @param hrId
     * @param roleIds
     * @return
     */
    @PutMapping("/updateHrRole")
    public ServerResponse updateHrRole(Integer hrId,Integer[] roleIds){
        return hrService.updateHrRole(hrId,roleIds);
    }

    @DeleteMapping("/{hrId}")
    public ServerResponse deleteHr(@PathVariable("hrId") Integer hrId){
        if(hrService.deleteHr(hrId)==1){
            return ServerResponse.success("删除成功");
        }
        return ServerResponse.error(1000,"删除失败");
    }
}
